home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Talking Telnet / source / init / init.c < prev    next >
C/C++ Source or Header  |  1996-06-22  |  14KB  |  480 lines

  1. /*
  2. *    init.c
  3. *    Top level of init code & general initialization routines
  4. *    Called only by main()
  5. *
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. *  Revisions:
  19. *  7/92        Telnet 2.6:    Initial version.  Jim Browne
  20. */
  21.  
  22.  
  23.  
  24. #include "init.proto.h"
  25.  
  26. #include "mainseg.proto.h"        /* For forcequit() proto */
  27. #include "iNetwork.proto.h"        /* For initnet proto */
  28. #include "macros.proto.h"        /* For initmacro proto */
  29. #include "wind.h"                /* For WindRec definition */
  30. #include "rsinterf.proto.h"        /* For RSgetwindow & RSnewwindow protos */
  31. #include "DlogUtils.proto.h"    /* For VersionNumber, UItemAssign, GetNewMyDialog protos */
  32. #include "Sets.proto.h"            /* For readconfig proto */
  33. #include "prefs.proto.h"
  34. #include "vr.h"                    /* For VRinit proto */
  35. #include "vsdata.h"
  36. #include "vsinterf.proto.h"        /* For VSinit proto */
  37. #include "vgtek.proto.h"        /* For VGinit proto */
  38. #include "vrrgmac.proto.h"        /* For MacRGinit proto */
  39. #include "menuseg.proto.h"
  40. #include "translate.proto.h"
  41. #include "tnae.h"
  42. #include "authencrypt.h"
  43. #include "authencrypt.proto.h"
  44. #include "ae.proto.h"            /* For AppleEvent handlers */
  45. #include "MacBinary.h"
  46. #include "binsubs.proto.h"        /* For init_mb_files proto */
  47. #include "drag.proto.h"            /* For MyDragInit */
  48. #include "IConfig.proto.h"
  49. #include "errors.proto.h"
  50. #include "telneterrors.h"
  51. #include "wdefpatch.proto.h" //for wdefLoad
  52. #include "memory.proto.h"
  53. #include "AddressXLation.h"
  54. /*--------------------------------------------------------------------------------*/
  55. #define CurrentVersion 1        /* Last known SysEnvirons version */
  56. #define HFSPtr ((long *)1014)
  57.  
  58. //extern    char *tempspot;             /* for temp storage only */
  59.  
  60. extern    Cursor *theCursors[];
  61.  
  62. extern    WindRec
  63.     *screens,        /* Window Records (VS) for :    Current Sessions */
  64.     *ftplog;        /*                                FTP Log             */
  65. extern    MenuHandle    myMenus[];
  66.  
  67. OSErr        io;
  68. SysEnvRec    theWorld;                        /* System Environment record */
  69. short        TempItemsVRefNum;
  70. long        TempItemsDirID;
  71. Boolean        gKeyboardHasControlKey, gAEavail;
  72. short **topLeftCorners;  //CCP 2.7: Better window positioning scheme
  73.  
  74.  
  75. Boolean encryptOK;                // true if des encryption routines available
  76. Boolean authOK;                    // true if authentication driver available
  77.  
  78. /*    checkCONF - check if the user clicked on a configuration file */
  79. void checkCONF( void)
  80. {
  81.     short    i,mess,count;
  82.     AppFile    theFile;
  83.     FSSpec    FileSpec;
  84.     long    junk = 0;
  85.  
  86.     if (gAEavail) return;    // If AppleEvents are available, we'll get ODOCs
  87.  
  88. #ifndef __powerpc__
  89.     CountAppFiles(&mess,&count);
  90.     if (mess==1) return;
  91.     if (count<1) return;
  92.     for(i=1;i<=count;i++) {
  93.         GetAppFiles(i, &theFile);
  94.         if (theFile.fType=='CONF') {
  95.             ClrAppFiles(i);
  96.             BlockMove(&theFile.fName, FileSpec.name, (*theFile.fName)+1); // pstring copy theFile.fName -> FileSpec.name
  97.             GetWDInfo(theFile.vRefNum, &FileSpec.vRefNum, &FileSpec.parID, &junk);
  98.             readconfig(FileSpec);    /* BYU LSC */
  99.             }
  100.         }
  101. #endif
  102.     return;
  103. }
  104.  
  105. void initEvents( void)
  106. {
  107.     EventRecord myEvent;
  108.  
  109.     FlushEvents(everyEvent - diskEvt,0);    /* Don't Let 'em get off without seeing it. */
  110.  
  111.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);    /* WNE always avail */
  112.     WaitNextEvent(everyEvent, &myEvent, gApplicationPrefs->TimeSlice, 0L);
  113. }
  114.  
  115. /* initftplog - initialize the FTP log window */
  116. void initftplog( void)
  117. {
  118.     Rect prect;
  119.     TerminalPrefs **termHdl;
  120.     Boolean scratchBoolean;
  121.     Str255 tempString;
  122.     
  123.     SetRect(&prect, 300,256,512,384);
  124.  
  125.     ftplog = (WindRec *) myNewPtr(sizeof(WindRec));
  126.     
  127.     GetIndString(tempString,MISC_STRINGS,FTP_LOG_STRING);
  128.     ftplog->vs=RSnewwindow( &prect, 50, 80, 24,
  129.                     tempString, 1, DefFONT, DefSIZE, gFTPServerPrefs->ShowFTPlog,
  130.                         1,0,0);    /* NCSA 2.5 */
  131.  
  132.     ftplog->wind = RSgetwindow( ftplog->vs);
  133.     ((WindowPeek)ftplog->wind)->windowKind = WIN_LOG;
  134.     
  135.     UseResFile(TelInfo->SettingsFile);
  136.     termHdl = (TerminalPrefs **)Get1NamedResource
  137.                 (TERMINALPREFS_RESTYPE, "\p<Default>");
  138.     DetachResource((Handle) termHdl);
  139.     HLock((Handle)termHdl);
  140.     
  141.     scratchBoolean = RSsetcolor( ftplog->vs, 0, (*termHdl)->nfcolor);
  142.     scratchBoolean = RSsetcolor( ftplog->vs, 1, (*termHdl)->nbcolor);
  143.     scratchBoolean = RSsetcolor( ftplog->vs, 2, (*termHdl)->bfcolor);
  144.     scratchBoolean = RSsetcolor( ftplog->vs, 3, (*termHdl)->bbcolor);
  145.  
  146.     DisposeHandle((Handle)termHdl);
  147.     VSwrite(ftplog->vs,"\033[24;0H",7);        /* Move to bottom of screen */
  148.     ftplog->active=0;                         /* Meaningless */
  149.     ftplog->port=0;                            /* As is this */
  150.     ftplog->termstate=VTEKTYPE;
  151. }
  152.  
  153.   /* copies a specified cursor from a resource into a nonrelocatable block
  154.     and returns a pointer to it in *result. */
  155. void loadcursor( short CursorID, CursPtr *result)
  156. {
  157.     CursHandle tempcurs;
  158.     OSErr Err;
  159.  
  160.     *result = nil;
  161.     do /* once */
  162.       {
  163.         tempcurs = GetCursor(CursorID);
  164.         Err = ResError();
  165.         if (Err != noErr)
  166.             break;
  167.         *result = (CursPtr) myNewPtr(sizeof(Cursor));        /* BYU LSC */
  168.         Err = MemError();
  169.         if (Err != noErr)
  170.             break;
  171.         LoadResource((Handle) tempcurs); /* in case it was purged */
  172.         Err = ResError();
  173.         if (Err != noErr)
  174.             break;
  175.         BlockMove((Ptr) *tempcurs, (Ptr) *result, sizeof(Cursor));
  176.       }
  177.     while (false);
  178.     if (Err != noErr)
  179.       {
  180.         if (*result != nil)
  181.             DisposPtr((Ptr) *result);
  182.         *result = &qd.arrow;
  183.       }
  184. } /* loadcursor */
  185.  
  186. /* initmcurs - Set up the mouse Cursors
  187.  *        NOTE: debug tells if the bug or the pointer is to be used as dflt.*/
  188. void initmcurs ( short debug) 
  189. {
  190.     loadcursor(rWatchCursor, &theCursors[watchcurs]);
  191.     loadcursor(rCrossCursor, &theCursors[graphcurs]);
  192.     loadcursor(rPOSCURS, &theCursors[poscurs]);
  193.     loadcursor(rBeamCursor, &theCursors[textcurs]);
  194.     loadcursor(rGINCURS, &theCursors[gincurs]);
  195.     loadcursor(rXferCURS, &theCursors[xfercurs]);
  196.     if (debug)
  197.         loadcursor(rDbugCURS, &theCursors[normcurs]);
  198.     else
  199.         theCursors[normcurs] = &qd.arrow;
  200.     SetCursor(theCursors[normcurs]);
  201. }
  202.  
  203. /*     If a file with the filetype "DBUG" was sent to us, set the debug mode. */
  204. short checkdebug( void)
  205. {
  206.     short i,mess,count;
  207.     AppFile theFile;
  208.     long junk = 0;
  209.  
  210.     if (gAEavail) return(FALSE);    //     Don't use CountAppFiles if AppleEvents
  211.                                     //    are available.
  212. #ifndef __powerpc__    
  213.     CountAppFiles(&mess,&count);
  214.     if (mess==1) return(FALSE);
  215.     if (count<1) return(FALSE);
  216.     for(i=1;i<=count;i++) {
  217.         GetAppFiles(i, &theFile);
  218.         if (theFile.fType=='DBUG') {
  219.             ClrAppFiles(i);
  220.             return(TRUE);
  221.             }
  222.         }
  223.     return(FALSE);
  224. #endif
  225. }
  226.  
  227. void InquireEnvironment( void)
  228. {
  229.     OSErr    err;
  230.     Boolean HFSflag;
  231.     long    response;
  232.     
  233.     long *HFSp=(long *)1014L;
  234.     
  235.     HFSflag= (*HFSp) > 0L;
  236.     if (!HFSflag) FatalAlert(NEED_HFS_ERR, 0, 0);
  237.  
  238.     err = SysEnvirons(CurrentVersion, &theWorld);
  239.     
  240.     if (err == envVersTooBig) FatalAlert(SYS_ENVIRON_ERR, 0, 0);
  241.     
  242.     if (theWorld.systemVersion < 0x0600) FatalAlert(SYSTEM_VERS_ERR, 0, 0);
  243.     
  244.     if (theWorld.machineType < 0) FatalAlert(ROM_VERS_ERR, 0, 0);
  245.     
  246.     // If there is a problem w/Gestalt, assume our keyboard has a Control key.
  247.     // Otherwise, we assume we have a control key unless a Mac or MacPlus keyboard is
  248.     // present.
  249.         
  250.     if ((err = Gestalt(gestaltKeyboardType, &response)) != noErr)
  251.         gKeyboardHasControlKey = TRUE;
  252.     else
  253.         gKeyboardHasControlKey = !(    (response == gestaltMacKbd) ||
  254.                                     (response == gestaltMacAndPad) ||
  255.                                     (response == gestaltMacPlusKbd) );
  256.     {
  257.         short virtualCode = 0;
  258.         Ptr KCHRPtr;
  259.         long newStuff;
  260.         unsigned long state = 0;
  261.         virtualCode = 0x10;//this will be a 'y' for Querty
  262.         KCHRPtr  = (Ptr)GetScriptManagerVariable(smKCHRCache);
  263.         newStuff = KeyTranslate(KCHRPtr,virtualCode,&state);
  264.         newStuff &= 0xFF; //only look at bottom byte
  265.         TelInfo->isQuerty = ((unsigned char) newStuff == 'y');
  266.     }
  267.  
  268.     TelInfo->haveColorQuickDraw = theWorld.hasColorQD;
  269. }
  270.  
  271. #define    kURLEventClass    'GURL'
  272. #define    kGetURLEvent    'GURL'
  273.  
  274. void initmac( void)
  275. {
  276.     EventRecord myEvent;
  277.     short     i;
  278.     long    gestaltvalue;
  279.     OSErr    err;
  280.     Handle    tempHandle;
  281.     InitGraf(&qd.thePort);
  282.     InitFonts();
  283.     InitWindows();
  284.     InitMenus();
  285.     TEInit();
  286.     InitDialogs(0L);                /* No resume proc */
  287.     InitCursor();
  288.     
  289.     TelInfo = (TelInfoRec *)myNewPtr(sizeof(TelInfoRec));
  290.     //tempspot = (char *)myNewPtr(256);
  291.     tempHandle = myNewHandle(sizeof(ApplicationPrefs));
  292.     HLockHi(tempHandle);
  293.     gApplicationPrefs = (ApplicationPrefs *) *tempHandle;
  294.     
  295.     tempHandle = myNewHandle(sizeof(FTPServerPrefs));
  296.     HLockHi(tempHandle);
  297.     gFTPServerPrefs = (FTPServerPrefs *)*tempHandle;
  298.     
  299.     screens = (WindRec *)myNewPtr(MaxSess*sizeof(WindRec));
  300.     for (i = 0; i < MaxSess; i++)
  301.         screens[i].active = CNXN_NOTINUSE;
  302.         
  303.     topLeftCorners = (short **) myNewHandle(MaxSess*sizeof(short));
  304.     
  305.  
  306.     InquireEnvironment();
  307.  
  308.     for (i=1;i<12;i++)
  309.         EventAvail( 0xffff, &myEvent);
  310.     
  311.     err = Gestalt(gestaltAppleEventsAttr, &gestaltvalue);        // See if AppleEvents are available
  312.     gAEavail = (!err && ((gestaltvalue >> gestaltAppleEventsPresent) & 0x0001));
  313.     
  314.     if (gAEavail) {
  315.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  316.                                             MyHandleOAppUPP,0,FALSE)) != noErr)
  317.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  318.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  319.                                             MyHandleODocUPP,0,FALSE)) != noErr)
  320.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  321.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  322.                                             MyHandlePDocUPP,0,FALSE)) != noErr)
  323.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  324.         if ((err = AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  325.                                             MyHandleQuitUPP,0,FALSE)) != noErr)
  326.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  327.         if ((err = AEInstallEventHandler(kURLEventClass,kGetURLEvent,
  328.                                             MyHandleGURLUPP,0,FALSE)) != noErr)
  329.             FatalAlert(AE_PROBLEM_ERR, 0, 0);
  330.         }
  331.  
  332.     TelInfo->screenRect = qd.screenBits.bounds;            /* well, they have to be set */
  333.                                                 /* somewhere, where else ? */
  334.     TelInfo->greyRegion = GetGrayRgn();
  335.     SetRect(&TelInfo->dragRect, 4, 24, TelInfo->screenRect.right-4,
  336.                         TelInfo->screenRect.bottom-4);
  337. }
  338.  
  339. void DoTheGlobalInits(void)
  340. {
  341.     long junk = 0;
  342.     ParamBlockRec pb;
  343.     
  344.     TelInfo->ScrlLock=0;
  345.     TelInfo->ftplogon=0;
  346.     TelInfo->done=0;
  347.     TelInfo->MacBinary=0;
  348.     TelInfo->numwindows=0;
  349.     TelInfo->CONFstate=0;
  350.     TelInfo->CONFactive=0;
  351.     TelInfo->SettingsFile=-1;
  352.     TelInfo->ApplicationFile = CurResFile();
  353.     TelInfo->ginon   = 0;
  354.     TelInfo->xferon  = 0;
  355.     TelInfo->graphs = 0;
  356.     TelInfo->debug = 1;
  357.     TelInfo->myfronttype = NO_WINDOW;
  358.     TelInfo->myfrontvs = 0;
  359.     TelInfo->suspended = FALSE;
  360.     TelInfo->myfrontwindow = 0L;    
  361.     TelInfo->myfrontRgn = 0L;    
  362.     TelInfo->lastCursor = 0L;
  363.     TelInfo->DefaultDirVRefNum = -1;
  364.     TelInfo->FTPClientVRefNum = -1;
  365.     TelInfo->DefaultDirDirID = 2;
  366.     TelInfo->FTPClientDirID = 2;
  367.     
  368.     pb.ioParam.ioNamePtr = 0L;
  369.     PBGetVol(&pb, FALSE);            /* see Tech Note 140 for implications of this */
  370.  
  371.     GetWDInfo(pb.ioParam.ioVRefNum, &(TelInfo->ApFolder.vRefNum), &(TelInfo->ApFolder.parID), &junk);
  372.     TelInfo->ApFolder.name[0] = 0;
  373.  
  374.     FindFolder( kOnSystemDisk, kSystemFolderType, kCreateFolder,
  375.                     &(TelInfo->SysFolder.vRefNum), &(TelInfo->SysFolder.parID));
  376.     TelInfo->SysFolder.name[0]=0;
  377.  
  378.     FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  379.                     &(TelInfo->PrefFolder.vRefNum), &(TelInfo->PrefFolder.parID));
  380.     TelInfo->PrefFolder.name[0]=0;
  381.     
  382.     FindFolder( kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &TempItemsVRefNum, &TempItemsDirID);
  383. }
  384.  
  385. void init (void)
  386. {
  387.     DialogPtr     dtemp;        /* Used for dialog display */
  388.     Size     junk;
  389.     long    junk2;
  390.     FlushEvents(everyEvent,0);
  391.     initmac();                /* initialize Macintosh stuff */
  392.     
  393.     DoTheGlobalInits();
  394.  
  395.     OpenPreferencesFile();
  396.     LoadPreferences();
  397.     UnloadSeg(&LoadPreferences);
  398.  
  399.     
  400.     dtemp = GetNewMyDialog(FirstDLOG, NULL, kInFront, (void *)ThirdCenterDialog);    /* opening dialog */
  401.     UItemAssign( dtemp, 2, VersionNumberUPP);
  402.     DrawDialog(dtemp);                                        /* while we init everything */
  403.     scriptKbdInit(); 
  404.     TelInfo->debug=checkdebug();    /* must return TRUE or FALSE */
  405.  
  406.     initmcurs(TelInfo->debug);        /* init the mouse cursor */
  407.     SetCursor(theCursors[watchcurs]);
  408.     setupmenu(gApplicationPrefs->CommandKeys);
  409.     
  410.     trInit(myMenus[National]);    /* LU: Initialize translation routines; must be done */
  411.                                 /* LU: prior to checkdebug(), since even the debug      */
  412.                                 /* LU: output goes through the translation routines. */
  413.     
  414.         
  415.     if (VSinit(MaxSess+2)) {        /* initialize the VS (which also inits RS stuff) */
  416.         DoError(104 | MEMORY_ERRORCLASS, LEVEL3, NULL);
  417.         forcequit();
  418.     }
  419.     //UnloadSeg(&VSinit);
  420.     //MaxMem(&junk2);
  421.  
  422.     InitDebug();
  423.     initftplog();                    /* allocate/initialize ftp log screen */
  424.  
  425.     VGinit();                        /* initialize Virtual Graphics after VS so that */
  426.                                     /* init messages can come on console screen */
  427.     VRinit();                        /* ICR init */
  428.     MacRGinit();                    /* ditto */
  429.     init_mb_files();                /* Initialize file id's to closed state */
  430.  
  431.     switchMenus(gApplicationPrefs->CommandKeys);
  432.  
  433.     TelInfo->done = FALSE;            // We're not quitting yet.
  434.  
  435.     SetCursor(theCursors[normcurs]);
  436.  
  437.     initmacros();
  438.  
  439.     ICinit();
  440.     UnloadSegments(); //get all of them at once
  441.     MaxMem(&junk);       //so DNR gets locked high in memory
  442.     io = OpenResolver(NULL);
  443.  
  444.     if (io) {                                
  445.         DialogPtr theErrDialog;
  446.         short itemhit = 0;
  447.  
  448.         theErrDialog = GetNewDialog(RESOLVERERROR, NULL, kInFront);
  449.         DrawDialog(theErrDialog);
  450.         while (!itemhit)
  451.             ModalDialog(DLOGwOKUPP,&itemhit);
  452.         DisposDialog(theErrDialog);
  453.         }
  454.  
  455.     initnet();                        /* initialize network stuff */
  456.  
  457.     updateMenuChecks();
  458.     DoTheMenuChecks();
  459.  
  460.     MyDragInit();
  461.     
  462.     checkCONF();                     /* Did user click on a set? */
  463.  
  464.     DisposDialog(dtemp);            /* Remove the splashbox...  */
  465.  
  466.        UnloadSeg(&init_mb_files);//this allows the authencrypt stuff to be locked above the FTP code segment
  467.     UnloadSeg(&MyDragInit);
  468.     MaxMem(&junk2); 
  469.  
  470.     encryptOK = authOK = true;
  471.     if (!auth_encrypt_init()) {
  472.         encryptOK = false;
  473.         authOK = false;
  474.     }
  475.     loadWDEF(); //this just loads the WDEF code in so that it doesnt fragment the heap later
  476.     loadErrors(); //ditto for the error code
  477.     
  478.     initMemoryBuffer(5*1024,30*1024); //this gives use a grow zone for emergency situations
  479. }
  480.